home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / TGE129C.ZIP / SOURCE / SCALE.C < prev    next >
C/C++ Source or Header  |  1993-08-20  |  3KB  |  81 lines

  1. /*****************************************************************************
  2. *       The Graphics Engine version 1.29ßC                                   *
  3. *                                                                            *
  4. *       The Graphics Engine code and documentation are Copyright (c) 1993    *
  5. *       by Matthew Hildebrand.                                               *
  6. *                                                                            *
  7. *       Unauthorised usage or modification of any or all of The Graphics     *
  8. *       Engine is strictly prohibited.                                       *
  9. *****************************************************************************/
  10.  
  11. /* Thanks are due to Thad Smith for helping to optimize this routine. */
  12.  
  13. #include <string.h>
  14.  
  15.  
  16. #ifdef __BORLANDC__
  17.   #pragma option -h-
  18. #endif
  19.  
  20.  
  21. void scaleBitmap(void *src, unsigned newWide, unsigned newDeep, void *dest)
  22. {
  23.   int countX, countY, oldWide, oldDeep;
  24.   int ry;       /* residual for y aspect ratio */
  25.   /* Source and destination pointers:
  26.   ** far pointers are used for each line, huge pointers traverse the
  27.   ** entire area, since it can be larger than 64k.
  28.   */
  29.   unsigned char huge *ph = (unsigned char huge*)dest + 4;
  30.   unsigned char huge *qh = (unsigned char huge*)src  + 4;
  31.   unsigned char far  *pf = (unsigned char far*) ph;
  32.   unsigned char far  *qf = (unsigned char far*) qh;
  33.   unsigned char far  *prev_pf = 0;      /* ptr to previous row */
  34.  
  35.   /* Get dimension information from source image */
  36.   oldWide = ((unsigned*)src)[0];
  37.   oldDeep = ((unsigned*)src)[1];
  38.  
  39.   /* Store dimension information in destination image */
  40.   ((unsigned*)dest)[0] = newWide;
  41.   ((unsigned*)dest)[1] = newDeep;
  42.  
  43.   /* The initial value of the y residual depends on whether the image
  44.   ** is expanded or contracted.  This feels awkward to me, but it works.
  45.   */
  46.   if (oldDeep < newDeep)
  47.     ry = oldDeep;
  48.   else
  49.     ry = newDeep - 1;
  50.  
  51.   /* Main scaling loops */
  52.   for (countY=newDeep; --countY>=0;)
  53.   {
  54.     if ((ry -= oldDeep) > 0)
  55.     {
  56.       /* This new row is the same as the previous one.  Copy it. */
  57.       memcpy(pf, prev_pf, newWide);
  58.     }
  59.     else
  60.     {
  61.       /* Get pixel at a time from old row with scaling algorithm */
  62.       register int rx = newWide + oldWide - 1;
  63.  
  64.       prev_pf = pf;
  65.       for (countX=newWide; --countX>=0;)
  66.       {
  67.     if ((rx-=oldWide) < 0)
  68.     {
  69.       do
  70.         qf++;
  71.       while ((rx+=newWide) < 0);
  72.     }
  73.     *pf++ = *qf;
  74.       }
  75.       do
  76.     qf = (unsigned char far*) (qh += oldWide);
  77.       while ((ry+=newDeep) < 0);
  78.     }
  79.     pf = (unsigned char far*) (ph += newWide);
  80.   }
  81. }